package adke.models.powerups.ability;

import adke.Util;
import adke.listeners.PlayerListener;
import adke.managers.GamePowerupManager;
import adke.models.Player;
import adke.models.powerups.AbilityPowerup;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Timer;
import java.util.TimerTask;

public class InvincibilityPowerup extends AbilityPowerup implements PlayerListener
{
    // hij mag maar 1 keer gebruikt worden
    // het moet duidelijk zijn dat hij geactiveerd is
    private boolean used, activated;
    
    // timer die bijhoud hoelang het effect aanhoud
    private Timer timer;
    
    public InvincibilityPowerup(Player owner)
    {
        super(owner);
        used = false; activated = false;
    }
    
    public InvincibilityPowerup()
    {
        used = false; activated = false;
    }

    public void activate()
    {
        if (used) return;
        
        System.out.println(Util.getLogTimeString() + "invincibility started for: " + owner);
        
        used = true;
        activated = true;
        
        // de duur
        final long duration = GamePowerupManager.getInvincibilityDuration();
        
        Runnable r = new Runnable()
        {
            public void run()
            {
                deactivate();
            }
        };
        
        // runt r na *duration*ms
        Util.notify(duration, r);
    }

    public void deactivate()
    {
        activated = false;
        
        // haal uit de lijst
        owner.powerupUsed(this);
        
        System.out.println(Util.getLogTimeString() + "invincibility ended for: " + owner);
    }

    private void revivePlayer(Player possibleOwner)
    {
        if (activated && possibleOwner == owner)
        {
            owner.setAlive(true);
        }
    }
    
    // 
    public void playerDied(Player deadPlayer)
    {
//        revivePlayer(deadPlayer);
    }

    public void playerKilledPlayer(Player killer, Player victim)
    {
        revivePlayer(victim);
    }
}
